package com.hfjyz.registration.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.hfjyz.registration.command.AdminModifyCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.service.RegistService;



public class AdminModifyController extends SimpleFormController {
    private RegistService registService;

    /**
     * @return the registService
     */
    public RegistService getRegistService() {
        return registService;
    }

    /**
     * @param registService
     *            the registService to set
     */
    public void setRegistService(RegistService registService) {
        this.registService = registService;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#
     * referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object,
     * org.springframework.validation.Errors, int)
     */
    @SuppressWarnings("rawtypes")
    @Override
    protected Map referenceData(HttpServletRequest request, Object command,
            Errors errors) throws Exception {
        if (errors.hasErrors()) {
            return null;
        }
        AdminModifyCommand cmd = (AdminModifyCommand) command;
        String id = request.getParameter("managerid");
        if (id != null && id.length() > 0) {
            try {
                cmd.setId(Integer.valueOf(id));
            } catch (Exception e) {
            }
            return registService.initAdminEdit(cmd, errors);
        } else {
            return null;
        }
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        AdminModifyCommand cmd = (AdminModifyCommand) command;
        if (!errors.hasErrors()) {
			registService.loggin(request, ExamContants.LOGING_TYPE_INFO,
					(Integer) null, "管理员", cmd.getAdminId(),
					ExamContants.LOGING_OP_TYPE_EDIT, "管理员修改密码");
            registService.modifyAdminPassword(cmd, errors);
        }
        if (errors.hasErrors()) {
			registService.loggin(request, ExamContants.LOGING_TYPE_WARNING,
					(Integer) null, "管理员", cmd.getAdminId(),
					ExamContants.LOGING_OP_TYPE_EDIT, "管理员修改密码");
            return new ModelAndView(getFormView(), errors.getModel());
        } else {
            return new ModelAndView(getSuccessView(), errors.getModel());
        }
    }
}
